package elitech.vietnam.weatherfashion.tabs.today;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import com.koushikdutta.urlimageviewhelper.UrlImageViewHelper;

import elitech.vietnam.weatherfashion.R;
import elitech.vietnam.weatherfashion.backgroundtask.PostLikeTask;
import elitech.vietnam.weatherfashion.config.Const;
import elitech.vietnam.weatherfashion.config.Utils;
import elitech.vietnam.weatherfashion.config.GlobalValue;
import elitech.vietnam.weatherfashion.custom.LikeButton;
import elitech.vietnam.weatherfashion.dto.MemberDTO;
import elitech.vietnam.weatherfashion.dto.PostDTO;
import elitech.vietnam.weatherfashion.tabs.AbstractFragment;
import elitech.vietnam.weatherfashion.tabs.BaseContainerFragment;
import elitech.vietnam.weatherfashion.ultilities.MyDialogHandler;

public class TodayPagerFragment extends AbstractFragment implements View.OnClickListener {

	int page;

	private ImageView iv_image;
	private LikeButton button_like;
	private PostDTO data;

	public static TodayPagerFragment init(int val, PostDTO data) {
		TodayPagerFragment frm = new TodayPagerFragment();
		Bundle args = new Bundle();
		args.putInt("page", val);
		frm.setArguments(args);
		//frm.data = data;
		return frm;
	}

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		page = getArguments().getInt("page");
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
		View view = inflater.inflate(R.layout.tab_today_viewpager, container, false);
		setupView(view);

		iv_image = (ImageView) view.findViewById(R.id.today_viewpager_image);
		button_like = (LikeButton) view.findViewById(R.id.today_button_like);
		button_like.setUp(true);
		
		return view;
	}
	
	@Override
	public void onResume() {
		super.onResume();
		initViews();
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.today_viewpager_image:
			((BaseContainerFragment) getParentFragment().getParentFragment()).replaceFragment(TodayDetailFragment.init(this.data), true);
			break;

		case R.id.today_button_like:
			button_like.setEnabled(false);
			MemberDTO user = GlobalValue.getInstance(getActivity()).readUserInfo();
			if (user == null) {
				Utils.openLogin(getActivity());

			} else {
				String ms = getString(R.string.do_you_want_to_like_this_post_);
				if (data.isLiked())
					ms = getString(R.string.do_you_want_to_unlike_this_post_);
				new MyDialogHandler().confirm(getActivity(), getString(R.string.like_confirm), ms, getString(R.string.no), getString(R.string.yes), new Runnable() {
					@Override
					public void run() {
						new PostLikeTask(getActivity(), button_like, data, !data.isLiked()).execute();
						// data.setLiked(!data.isLiked());
					}
				}, null);
			}
			button_like.setEnabled(true);
			break;

		default:
			break;
		}
	}

	// Setup view data
	public void initViews() {
		if (this.data == null)
			return;
		Utils.Log("//// Init page: " + page);
		if (button_like != null) {
			button_like.setData(data.getLikes(), data.isLiked());
			button_like.setOnClickListener(this);
		}
		if (iv_image != null) {
			UrlImageViewHelper.setUrlDrawable(iv_image, Const.SERVER_IMAGE_URL + this.data.getImage(), R.drawable.loading_time);
			iv_image.setOnClickListener(this);
		}
	}

	public int getPageIndex() {
		return page;
	}

	public void setPageData(PostDTO data) {
		this.data = data;
		notifyDataChanged();
	}
	
	private void notifyDataChanged() {
		View view = getView();
		if (view != null) {
			iv_image = (ImageView) view.findViewById(R.id.today_viewpager_image);
			button_like = (LikeButton) view.findViewById(R.id.today_button_like);
			if (button_like != null) {
				button_like.setUp(true);
				button_like.setData(data.getLikes(), data.isLiked());
				button_like.setOnClickListener(this);
			}
			if (iv_image != null) {
				UrlImageViewHelper.setUrlDrawable(iv_image, Const.SERVER_IMAGE_URL + this.data.getImage(), R.drawable.loading_time);
				iv_image.setOnClickListener(this);
			}
		}
	}

	//
	// class GetLikedTask extends AsyncTask<Integer, Integer, Boolean> {
	// @Override
	// protected Boolean doInBackground(Integer... params) {
	// AppUtils.Log("Today Pager Fragment Loading...");
	// PHPSoapServiceCaller appService = new PHPSoapServiceCaller();
	// if (AppConfig.getInstance().isLoggedIn && AppConfig.getInstance().appUser
	// != null)
	// liked = appService.isLiked(AppConfig.getInstance().appUser.getId(),
	// data.getId());
	// return null;
	// }
	// @Override
	// protected void onPreExecute() {
	// super.onPreExecute();
	// button_like.setVisibility(View.INVISIBLE);
	// }
	// @Override
	// protected void onPostExecute(Boolean result) {
	// super.onPostExecute(result);
	// button_like.setVisibility(View.VISIBLE);
	// button_like.setData(data.getLikes(), liked);
	// }
	// }
	//
}
